package parsers;

import generators.MenuGenerator;

import java.util.List;
import java.util.Vector;

import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.xpath.XPath;

import types.Menu;
import types.MenuBar;
import types.MenuItem;

public class MenuParser extends Parser
{

	/**
	 * @param args
	 */
//	public static void main(String[] args)
//	{
//		MenuParser p = new MenuParser();
//		p.parse();
//
//	}
	
	public MenuBar parse(String fileName)
	{
		xpathQuery = "/Menues";
		
		 try
		{
			MenuBar bar;
			Vector<Menu> menues = new Vector<Menu>();
//			Menu menu = new Menu();
			MenuItem item = null;
			
			root = (Element) XPath.selectSingleNode(getXMLDoc(fileName), xpathQuery);
			List<Element> list = root.getChildren();
			for (int i = 0; i < list.size(); i++)
			{
				Element element = list.get(i);
				Menu menu = new Menu();
				int level = Integer.parseInt(element.getAttributeValue("level"));
				String text = element.getAttributeValue("text");
				menu.setText(text);
				menu.setLevel(level);
				
				Vector<MenuItem> items = new Vector<MenuItem>();
				menu.setItems(items);
				
				List<Element> l = element.getChildren();
				
				for (int j = 0; j < l.size(); j++)
				{
					Element tmp = l.get(j);
					item = new MenuItem(Integer.parseInt(tmp.getAttributeValue("level")), tmp.getAttributeValue("text"), tmp.getAttributeValue("Form"));
					items.addElement(item);
				}
				
				menues.addElement(menu);
			}
			
			bar = new MenuBar(menues);

			return bar;

		}
		catch (JDOMException e)
		{
			e.printStackTrace();
			return null;
		}
	}

}
